// pages/commodity/commodity.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Goods:[],
    show:false,
    order:[{
      'number':1,
      'color':"",
      'size':"",
      'rmb':0,
      'sale':0
    }
    ],
    Evaluate:[],
    allEvaluate:false,

    windowHeight:0
  },
  clear:function(){
    this.setData({
      allEvaluate:!this.data.allEvaluate
    })
  },
   // 收藏商品
   Collection:function(){
    var that=this
    wx.showLoading({
      title: '收藏中',
    })
    wx.cloud.callFunction({
      name: "login"
    }).then(res => {
      var openid=res.result.openid
      db.collection('User').where({
        _openid:openid
      }).get().then(res => {
        // res.data 包含该记录的数据
        console.log(res.data[0]._id)
        let i=0;
        for(  ;i<res.data[0].Collection.length;i++){
          if(that.data.Goods[0].id==res.data[0].Collection[i]){
            break;
          }
        }
        if(i==res.data[0].Collection.length){
          var Collection=res.data[0].Collection.concat(that.data.Goods[0].id)
          var title=res.data[0].title.concat(that.data.Goods[0].title)
          var img=res.data[0].img.concat(that.data.Goods[0].img[0])
          var rmb=res.data[0].rmb.concat(that.data.Goods[0].rmb)

          db.collection('User').doc(res.data[0]._id).update({
            data: {
              // 表示指示数据库将字段自增 10
              Collection: Collection,
              img:img,
              title:title,
              rmb:rmb
            },
            success: function(res) {
              console.log(res.data)
              wx.hideLoading();
              wx.showToast({
                title: "收藏成功",
                icon:'none'
              })
            }
          })
        }else{
          wx.hideLoading();
          wx.showToast({
            title: "已收藏",
            icon:'none'
          })
        }       
      })
    }).catch(err => {
      console.log(err);
    })
  },
  lt:function(){
    console.log("dsfdfdfsd")
    wx.navigateTo({
      url: "/pages/ChatRoom/ChatRoom?id=" + this.data.Goods[0].id + "",
    })
  },
  // 博文跳转
  blog:function(e){
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: "/pages/BlogDetails/BlogDetails?id=" + e.currentTarget.id
    })
  },
  shop:function(e){
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: "/pages/Shop/Shop?openid=" + e.currentTarget.id
    })
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  GenerateOrder:function(){
    wx.navigateTo({
      url: "/pages/GenerateOrder/GenerateOrder?id=" + this.data.Goods[0].id + "&number=" + this.data.order[0].number + "&color=" + this.data.order[0].color + "&size=" + this.data.order[0].size + "&rmb=" + this.data.order[0].rmb + "&sale=" + this.data.order[0].sale +"",
    })
  },
  show:function(){
    this.setData({
      show:!this.data.show
    })
  },
  color:function(e){
    console.log(e.target.dataset.id)
    this.setData({
      'order[0].color':e.target.dataset.id
    })
  },
  size:function(e){
    console.log(e.target.dataset.id)
    this.setData({
      'order[0].size':e.target.dataset.id
    })
  },
  jian:function(){
    if(this.data.order[0].number<=1){
      this.setData({
        'order[0].number':1
      })
    }else{
      this.setData({
        'order[0].number':this.data.order[0].number-1
      })
    }
  },
  jia:function(){
    this.setData({
      'order[0].number':this.data.order[0].number+1
    })
  },
 // 获取商品详细信息
  getGoods: function (id) {
    console.log(id)
    db.collection('Goods').doc(id)
      .get().then(res => {
        console.log(res.data.fileIDs)
        this.setData({
          'order[0].color':res.data.color[0],
          'order[0].size':res.data.size[0],
          'order[0].sale':res.data.sale,
          'Goods[0].img': res.data.fileIDs,
          'Goods[0].id': res.data._id,
          'Goods[0].openid': res.data._openid,
          'Goods[0].img2': res.data.fileIDs2,
          'Goods[0].title': res.data.title,
          'Goods[0].color': res.data.color,
          'Goods[0].rmb': res.data.rmb,
          'Goods[0].rmb2': (res.data.rmb/10*res.data.sale).toFixed(2),
          'order[0].rmb':res.data.rmb/10*res.data.sale,
          'Goods[0].sale': res.data.sale,
          'Goods[0].sell': res.data.sell,
          'Goods[0].liulan': res.data.liulan,
          'Goods[0].size':res.data.size,
          'Goods[0].address':res.data.address,
          'Goods[0].express':res.data.express,
        })
        console.log(this.data.Goods)
      })
      .catch(err => {
        console.error(err)
        wx.hideLoading();
      })
  },
// 获取评论信息
  getEvaluate:function(GoodsId){
    var that=this
    that.setData({
      Evaluate:[]
    })
    db.collection('Evaluate').where({
      GoodsId:GoodsId
    }).get().then(res => {
      // res.data 包含该记录的数据
      console.log(res.data)
      for(let i=0;i<res.data.length;i++){
        var id = "Evaluate[" + i + "].id"
        var openid = "Evaluate[" + i + "].openid"
        var avatarUrl = "Evaluate[" + i + "].avatarUrl"
        var nickName = "Evaluate[" + i + "].nickName"
        var title = "Evaluate[" + i + "].title"
        var GoodsImg = "Evaluate[" + i + "].GoodsImg"
        var GoodsRmb = "Evaluate[" + i + "].GoodsRmb"
        var GoodsTitle = "Evaluate[" + i + "].GoodsTitle"
        var OrderRmb = "Evaluate[" + i + "].OrderRmb"
        var OrderSize = "Evaluate[" + i + "].OrderSize"
        var OrderColor = "Evaluate[" + i + "].OrderColor"
        var OrderNumber = "Evaluate[" + i + "].OrderNumber"
        var img = "Evaluate[" + i + "].img"
        var one_1 = "Evaluate[" + i + "].one_1"
        var one_2 = "Evaluate[" + i + "].one_2"
        var one_3 = "Evaluate[" + i + "].one_3"
        var OrderId = "Evaluate[" + i + "].OrderId"
        var SellOpenid = "Evaluate[" + i + "].SellOpenid"
        var state = "Evaluate[" + i + "].state"
        var EvaluationTime = "Evaluate[" + i + "].EvaluationTime"
        var GoodsId = "Evaluate[" + i + "].GoodsId"
        that.setData({
          [id] :res.data[i]._id,
          [openid]:res.data[i]._openid,
          [avatarUrl]:res.data[i].avatarUrl,
          [nickName]:res.data[i].nickName,
          [title]:res.data[i].title,
          [GoodsImg]:res.data[i].GoodsImg,
          [GoodsRmb]:res.data[i].GoodsRmb,
          [GoodsTitle]:res.data[i].GoodsTitle,
          [OrderRmb]:res.data[i].OrderRmb,
          [OrderSize]:res.data[i].OrderSize,
          [OrderColor]:res.data[i].OrderColor,
          [OrderNumber]:res.data[i].OrderNumber,
          [img]:res.data[i].img,
          [one_1]:res.data[i].one_1,
          [one_2]:res.data[i].one_2,
          [one_3]:res.data[i].one_3,
          [OrderId]:res.data[i].OrderId,
          [SellOpenid]:res.data[i].SellOpenid,
          [state]:res.data[i].state,
          [EvaluationTime]:res.data[i].EvaluationTime,
          [GoodsId]:res.data[i].GoodsId,
        })
        console.log(that.data.Evaluate[i].img)
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    var that = this 
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.windowHeight)
          that.setData({
              windowHeight: res.windowHeight
          });
      }
    });
    db.collection('Bowman').where({
      GoodsId:db.command.in([options.commodity])
    }).get({
      success: res => {
        this.setData({
          blog:res.data
        })
        console.log(res.data);

      }
    })
    console.log(options.commodity)
    this.getGoods(options.commodity)
    this.getEvaluate(options.commodity)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      show:false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})